package com.example.analyzevisio.service;

import com.example.analyzevisio.model.component.Items;
import com.example.analyzevisio.model.node.*;
import com.example.analyzevisio.model.node.failnode.FailNode;
import com.example.analyzevisio.model.node.startnode.StartData;
import com.example.analyzevisio.model.node.startnode.StartNode;
import com.example.analyzevisio.model.component.Position;
import com.example.analyzevisio.model.component.Size;
import com.example.analyzevisio.model.node.failnode.FailData;
import com.example.analyzevisio.model.node.startnode.StateMachine;
import com.example.analyzevisio.model.node.startnode.StateProps;
import com.example.analyzevisio.model.node.succeednode.SucceedData;
import com.example.analyzevisio.model.node.succeednode.SucceedNode;
import com.example.analyzevisio.utis.Obtain;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.analyzevisio.utis.Obtain.*;

public class NodeService {
    private static HashMap<String, Map<String, String>> dataMap;


    public void setDataMap(HashMap<String, Map<String, String>> dataMap){
        this.dataMap = dataMap;
    }


    /**
     * 失败节点
     * @param shapeElement
     * @return
     */
    public static FailNode getFailNode(Element shapeElement,String text) {


        FailNode node = new FailNode();

        FailData failData = new FailData();
        failData.setType("Fail");
        failData.setLabel(text);
        String id = shapeElement.getAttribute("ID");
        failData.setStateId(dataMap.get(id).get("stateId"));
        failData.setStateType("Fail");
        HashMap<String, String> failMap = new HashMap<>();
        failMap.put("ErrorCode","");
        failMap.put("Message","");
        failData.setFailProps(failMap);
        node.setData(failData);

        failExtracted(shapeElement, node);


        return node;
    }

    /**
     * 开始节点获取元素
     * @param shapeElement
     * @return
     */
    public static StartNode getStartNode(Element shapeElement) {
        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        NodeList textNodes = shapeElement.getElementsByTagName("Text");

        StartNode node = new StartNode();

        //获取坐标和父节点
        Map<String, String> obtainCoordinates = getObtainCoordinates(shapeElement);
        double pinX = Double.parseDouble(obtainCoordinates.get("pinX"));
        double pinY = Double.parseDouble(obtainCoordinates.get("pinY"));

        node.setPosition(new Position(pinX,pinY));
        node.setParent(obtainCoordinates.get("parent"));

        Size size = new Size(
                40,
                40
        );

        node.setSize(size);
        node.setView("html-view");
        node.setShape("html");
        node.setId(substring);

        StartData startData = new StartData();
        startData.setType("node");
        startData.setLabel(getText(textNodes));
        String id = shapeElement.getAttribute("ID");
        startData.setStateId(dataMap.get(id).get("stateId"));
        startData.setStateType("Start");
        startData.setStateProps(
                new StateProps(
                        new StateMachine("测试","Test-001-1.0.0","1.0.0")
                ));
        node.setData(startData);

        Ports ports = new Ports();
        Groups groups = new Groups();
        Group1 group1 = new Group1();
        Attrs attrs = new Attrs();
        Circle circle = new Circle();
        circle.setR("3");
        circle.setMagnet(true);
        circle.setStroke("#31d0c6");
        circle.setStrokeWidth("2");
        circle.setFill("#fff");
        HashMap<String, String> map = new HashMap<>();
        map.put("visibility","hidden");
        circle.setStyle(map);
        attrs.setCircle(circle);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name","absolute");
        group1.setAttrs(attrs);
        group1.setPosition(hashMap);
        groups.setGroup1(group1);


        //获取连接点坐标
        List<Items> items = getConnectionPoint("起始");

        ports.setGroups(groups);
        ports.setItems(items);

        node.setPorts(ports);

        node.setZIndex("10");
        return node;
    }



    /**
     * 成功节点获取元素
     * @param shapeElement
     * @return
     */
    public static SucceedNode getSucceedNode(Element shapeElement,String text) {


        SucceedNode node = new SucceedNode();
        succeedExtracted(shapeElement, node);
        SucceedData succeedData = new SucceedData();
        succeedData.setType("Succeed");
        succeedData.setLabel(text);
        String id = shapeElement.getAttribute("ID");
        succeedData.setStateId(dataMap.get(id).get("stateId"));
        succeedData.setStateType("Succeed");
        node.setData(succeedData);

        return node;
    }

    private static void succeedExtracted(Element shapeElement, SucceedNode node) {
        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        //获取坐标和父节点
        Map<String, String> obtainCoordinates = getObtainCoordinates(shapeElement);
        double pinX = Double.parseDouble(obtainCoordinates.get("pinX"));
        double pinY = Double.parseDouble(obtainCoordinates.get("pinY"));

        node.setPosition(new Position(pinX,pinY));
        node.setParent(obtainCoordinates.get("parent"));

        Size size = new Size(
                40,
                40
        );

        node.setSize(size);
        node.setView("html-view");
        node.setShape("html");
        node.setId(substring);
        Ports ports = new Ports();
        Groups groups = new Groups();
        Group1 group1 = new Group1();
        Attrs attrs = new Attrs();
        Circle circle = new Circle();
        circle.setR("3");
        circle.setMagnet(true);
        circle.setStroke("#31d0c6");
        circle.setStrokeWidth("2");
        circle.setFill("#fff");
        HashMap<String, String> map = new HashMap<>();
        map.put("visibility","hidden");
        circle.setStyle(map);
        attrs.setCircle(circle);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name","absolute");
        group1.setAttrs(attrs);
        group1.setPosition(hashMap);
        groups.setGroup1(group1);

        //获取连接点坐标
        List<Items> items = getConnectionPoint("起始");

        ports.setGroups(groups);
        ports.setItems(items);

        node.setPorts(ports);

        node.setZIndex("10");
        List<String> matches = getStrings(shapeElement);
        for (String match : matches) {
            if (dataMap.containsKey(match)){
                Map<String, String> stringMap = dataMap.get(match);
                node.setParent(stringMap.get("UniqueID"));
            }
        }
    }
    private static void failExtracted(Element shapeElement, FailNode node) {
        String uniqueID = shapeElement.getAttribute("UniqueID");

        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        //获取坐标和父节点
        Map<String, String> obtainCoordinates = getObtainCoordinates(shapeElement);
        double pinX = Double.parseDouble(obtainCoordinates.get("pinX"));
        double pinY = Double.parseDouble(obtainCoordinates.get("pinY"));

        node.setPosition(new Position(pinX,pinY));
        node.setParent(obtainCoordinates.get("parent"));
        Size size = new Size(
                40,
                40
        );

        node.setSize(size);
        node.setView("html-view");
        node.setShape("html");
        node.setId(substring);
        Ports ports = new Ports();
        Groups groups = new Groups();
        Group1 group1 = new Group1();
        Attrs attrs = new Attrs();
        Circle circle = new Circle();
        circle.setR("3");
        circle.setMagnet(true);
        circle.setStroke("#31d0c6");
        circle.setStrokeWidth("2");
        circle.setFill("#fff");
        HashMap<String, String> map = new HashMap<>();
        map.put("visibility","hidden");
        circle.setStyle(map);
        attrs.setCircle(circle);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name","absolute");
        group1.setAttrs(attrs);
        group1.setPosition(hashMap);
        groups.setGroup1(group1);

        //获取连接点坐标
        List<Items> items = getConnectionPoint("起始");
        ports.setGroups(groups);
        ports.setItems(items);

        node.setPorts(ports);

        node.setZIndex("10");
        List<String> matches = getStrings(shapeElement);
        for (String match : matches) {
            if (dataMap.containsKey(match)){
                Map<String, String> stringMap = dataMap.get(match);
                node.setParent(stringMap.get("UniqueID"));
            }
        }
    }
}
